#include "frpconfig.h"
#include <QFile>
#include <QDebug>
#include "mainform.h"
FrpConfig::FrpConfig(){

}

QString FrpConfig::getFrpConfig(){

    QString strs;
    QFile Cache(CachePath);
    if( !Cache.open(QIODevice::ReadOnly)){
        qDebug() << "文件打开失败 ";
        return NULL;
    }
    QTextStream stream(&Cache);
    while(!stream.atEnd()){
        QString line = stream.readLine();
        strs.push_back(line);
        strs.push_back("\r\n");
    }
    Cache.close();
    return strs;
}

void FrpConfig::setFrpConfig(QString tmp){
    QFile Cache(CachePath);
    if( !Cache.open(QIODevice::WriteOnly)){
        qDebug() << "文件打开失败";
    }
    QTextStream stream(&Cache);
    stream << tmp << "\n";
    Cache.close();
}

QString FrpConfig::setCFG(int baseNum,int xtcpNum){
    QString frpCommon;
    frpCommon = "[common]\r\nserver_addr="
                +mainw->frpCommon.addr+"\r\n"
                +"server_port="
                +mainw->frpCommon.port+"\r\n"
                +"token="
                +mainw->frpCommon.token+"\r\n"
                +"\r\n";
    /*基础模式*/
    QList<QString> baseRD(50);
    for(int i = 0;i < baseNum ;i++){
        baseRD[i] =
                +"["+mainw->frpBase[i].name+"_"+mainw->frpBase[i].type+"]"    +"\r\n"
                +"type="+mainw->frpBase[i].type                               +"\r\n"
                +"local_ip="+mainw->frpBase[i].ip                             +"\r\n"
                +"local_port="+mainw->frpBase[i].localPort                    +"\r\n"
                +"remote_port="+mainw->frpBase[i].remotePort                  +"\r\n"
                +"use_encryption="+mainw->frpBase[i].use_encryption           +"\r\n"
                +"use_compression="+mainw->frpBase[i].use_compression         +"\r\n\r\n";
    }
    for(int i = 0 ; i < baseNum ; i++)
        frpCommon.append(baseRD[i]);

    /*p2p xtcp模式*/
    QList<QString> xtcpRD(50);
     for(int i = 0;i < xtcpNum ;i++){
        if(mainw->frpXtcp[i].sORc == "server"){         //服务端
            xtcpRD[i] = "["+mainw->frpXtcp[i].name+"_xtcp"+"]"                +"\r\n"
                        +"type=xtcp"                                          +"\r\n"
                        +"sk="+mainw->frpXtcp[i].sk                           +"\r\n"
                        +"local_port="+mainw->frpXtcp[i].port                 +"\r\n"
                        +"use_encryption="+mainw->frpXtcp[i].use_encryption   +"\r\n"
                        +"use_compression="+mainw->frpXtcp[i].use_compression +"\r\n"
                        +"local_ip="+ mainw->frpXtcp[i].ip                    +"\r\n\r\n";
        }

        else if(mainw->frpXtcp[i].sORc == "client" ){   //客户端
            xtcpRD[i] = "["+mainw->frpXtcp[i].name+"_xtcp"+"_visitor"+"]"     +"\r\n"
                        +"type=xtcp"                                          +"\r\n"
                        +"bind_addr = "+mainw->frpXtcp[i].ip                +"\r\n"
                        +"role=visitor"                                       +"\r\n"
                        +"server_name="+mainw->frpXtcp[i].name+"_xtcp"        +"\r\n"
                        +"bind_port="+mainw->frpXtcp[i].port                  +"\r\n"
                        +"use_encryption="+mainw->frpXtcp[i].use_encryption   +"\r\n"
                        +"use_compression="+mainw->frpXtcp[i].use_compression +"\r\n"
                        +"sk="+mainw->frpXtcp[i].sk                           +"\r\n\r\n";
        }
     }
     for(int i = 0 ; i < xtcpNum ; i++)
         frpCommon.append(xtcpRD[i]);
    return frpCommon;
}
